Micron Document
🎖️GitЯра🎖️


Displaying Raw • Download

core/database/src/commonMain/kotlin/org/meshtastic/core/database/DbFlowRecovery.kt 41414f832f938b655c97bc07509bc140ea16e2a3 (41414f83) Text, 3.34 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.database

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.delay
Tff7b72import T7ee787kotlinx.coroutines.flow.Flow
Tff7b72import T7ee787kotlinx.coroutines.flow.emitAll
Tff7b72import T7ee787kotlinx.coroutines.flow.flow
Tff7b72import T7ee787kotlinx.coroutines.flow.onEach
Tff7b72import T7ee787kotlinx.coroutines.flow.retryWhen

Tff7b72private Tff7b72const Tff7b72val Te6edf3INITIAL_DB_RETRY_DELAY_MS Tff7b72= T79c0ff1Te6edf3_000L
Tff7b72private Tff7b72const Tff7b72val Te6edf3MAX_DB_RETRY_DELAY_MS Tff7b72= T79c0ff3T79c0ff0Te6edf3_000L
Tff7b72private Tff7b72const Tff7b72val Te6edf3MAX_DB_RETRY_BACKOFF_SHIFT Tff7b72= T79c0ff5

T8b949e/**
* Restarts a database-backed Flow after the Room pool wedge behind #6608 — an acquire timeout or a stall — with capped
* exponential backoff. The backoff resets after a successful emission; every other failure propagates.
*
* [DatabaseManager.observeCurrentDb] recovers a wedged pool in place, but its per-collector and per-window budgets can
* exhaust while a leaked-permit storm is still active; a `stateIn(..., SharingStarted.Eagerly, ...)` upstream that then
* throws is dead for the process lifetime. Retrying re-enters recovery with a fresh collector once the manager's rate
* window reopens.
*
* Deliberately narrower than [DatabaseManager.isDbClosedException]: a closed pool is not retried here.
* `observeCurrentDb` re-latches on the replacement pool by itself, so retrying adds nothing — and it would keep a flow
* alive that used to terminate, turning a dead collector into a backoff loop that outlives its collecting scope.
*/
Tff7b72fun Tff7b72<Te6edf3TTff7b72> Td2a8ffFlowTff7b72<Te6edf3TTff7b72>Tb4b4b4.Te6edf3retryOnDbPoolFailureTb4b4b4(Te6edf3labelTb4b4b4: Tffa657StringTb4b4b4)Tb4b4b4: Te6edf3FlowTff7b72<Te6edf3TTff7b72> Tff7b72= Te6edf3flow Tb4b4b4{
T8b949e// Per-collection state: each collector backs off independently, and a plain var is safe because a Flow is
T8b949e// collected by one coroutine at a time.
Tff7b72var Te6edf3consecutiveFailures Tff7b72= T79c0ff0
Te6edf3emitAllTb4b4b4(
Te6edf3onEach Tb4b4b4{ Te6edf3consecutiveFailures Tff7b72= T79c0ff0 Tb4b4b4}
Tb4b4b4.Te6edf3retryWhen Tb4b4b4{ Te6edf3causeTb4b4b4, Te6edf3_ Tff7b72-Tff7b72>
Tff7b72val Te6edf3recoverable Tff7b72=
Te6edf3cause Tff7b72is Te6edf3DbFlowStalledException Tff7b72|Tff7b72|
Tb4b4b4(Te6edf3cause Tff7b72is Te6edf3Exception Tff7b72&Tff7b72& Te6edf3DatabaseManagerTb4b4b4.Te6edf3isDbPoolAcquireTimeoutExceptionTb4b4b4(Te6edf3causeTb4b4b4)Tb4b4b4)
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3recoverableTb4b4b4) Tff7b72returnTf0883e@retryWhen Tff7b72false
Te6edf3consecutiveFailures Tff7b72+Tff7b72= T79c0ff1
Tff7b72val Te6edf3delayMs Tff7b72=
Tb4b4b4(Te6edf3INITIAL_DB_RETRY_DELAY_MS Te6edf3shl Tb4b4b4(Te6edf3consecutiveFailures Tff7b72- T79c0ff1Tb4b4b4)Tb4b4b4.Te6edf3coerceAtMostTb4b4b4(Te6edf3MAX_DB_RETRY_BACKOFF_SHIFTTb4b4b4)Tb4b4b4)
Tb4b4b4.Te6edf3coerceAtMostTb4b4b4(Te6edf3MAX_DB_RETRY_DELAY_MSTb4b4b4)
Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3causeTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ffDB flow 'Tffd700$Te6edf3labelTa5d6ff' failed with a recoverable pool error; retrying in Tffd700${Te6edf3delayMsTffd700}Ta5d6ffmsTa5d6ff" Tb4b4b4}
Te6edf3delayTb4b4b4(Te6edf3delayMsTb4b4b4)
Tff7b72true
Tb4b4b4}Tb4b4b4,
Tb4b4b4)
Tb4b4b4}

T8b949e/** Signals that a database-backed Flow never produced its first value, i.e. the Room pool is not serving queries. */
Tff7b72class T56d364DbFlowStalledExceptionTb4b4b4(Te6edf3messageTb4b4b4: Tffa657StringTb4b4b4) Tb4b4b4: Te6edf3IllegalStateExceptionTb4b4b4(Te6edf3messageTb4b4b4)

Served by rngit 1.5.0 - Generated in 0.04s